package com.t282.crm.service.impl;

import com.t282.crm.entity.Role;
import com.t282.crm.entity.User;
import com.t282.crm.repository.UserRepository;
import com.t282.crm.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public User login(String usrName, String usrPassword) {
        return userRepository.getByUsrNameAndUsrPassword(usrName,usrPassword);
    }

    @Override
    @CachePut(value = "user",keyGenerator = "keyGenerator")
    public User addUser(User user) {
        return userRepository.save(user);
    }

    @Override
    /*@CacheEvict(value = "user",allEntries=true)*/
    public void deleteUser(Long usrId) {
        userRepository.deleteById(usrId);
    }

    @Override
    @CacheEvict(value = "user",key = "#user.usrId")
    public User updateUser(User user) {
        return userRepository.save(user);
    }

    @Override
    @Cacheable(value = "user",keyGenerator = "keyGenerator")
    public User getUser(Long usrId) {
        return userRepository.findById(usrId).get();
    }

    @Override
    public Page<User> findAllUsers(String usrName, Long roleId, Pageable pageable) {
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(usrName != null && !"".equals(usrName)){
                    predicates.add(criteriaBuilder.like(root.get("usrName"),"%"+usrName+"%"));
                }
                if(roleId != null && roleId.longValue() != 0){
                    Role role = new Role();
                    role.setRoleId(roleId);
                    predicates.add(criteriaBuilder.equal(root.get("role"),role));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return userRepository.findAll(specification,pageable);
    }

    @Override
    public User getUserByUsrName(String usrName) {
        return userRepository.findUserByUsrName(usrName);
    }

    @Override
    public String encryptPassword(Object plaintextPassword) throws IllegalArgumentException {
        String salt="czkt";
        Md5Hash md5Hash = new Md5Hash(plaintextPassword,salt,2);
        return md5Hash.toString();
    }
}
